<?php



namespace App\Model;

use App\Helpers\AliyunUtil;
use Illuminate\Database\Eloquent\Model;

/**
 * 动态
 */
class Moment extends Model
{
    protected $table = 'moment';

    //正常
    const STATUS_OK = 1;
    //删除
    const STATUS_DEL = 0;
    //待审核
    const STATUS_VERIFING = -1;
    //审核不通过
    const STATUS_BAD = -2;
    const STATUS = [
        self::STATUS_OK => '正常',
        self::STATUS_VERIFING => '待审核',
        self::STATUS_DEL => '删除',
        self::STATUS_BAD => '审核不通过',
    ];

    //普通动态
    const TYPE_NORMAL = 1;
    //真人头像分享动态
    const TYPE_REAL_AVATAR_SHARE = 2;
    //爱情宣言动态
    const TYPE_SELF_INTRO = 3;
    const TYPE = [
        self::TYPE_NORMAL => '普通动态',
        self::TYPE_REAL_AVATAR_SHARE => '真人头像分享动态',
        self::TYPE_SELF_INTRO => '爱情宣言动态',
    ];

    //动态缩略图-大
    const THUMB_SIZE_BIG = 500;
    //动态缩略图-小
    const THUMB_SIZE_SMALL = 250;

    protected $fillable = [
        "user_id", "status", "voice", "user_gender", "words", "images", "is_top"
    ];

    public function userItem()
    {
        return $this->belongsTo(User::class, 'user_id', 'id');
    }

    public function userProfile()
    {
        return $this->belongsTo(UserProfile::class, 'user_id', 'user_id');
    }

    public function setImagesAttribute($images)
    {
        if (is_array($images)) {
            $this->attributes['images'] = json_encode($images);
        }
    }

    public function getImagesAttribute($images)
    {
        return json_decode($images, true);
    }

    /**
     * 获取数据
     */
    static function getItem($momentId = 0)
    {
        return self::find($momentId);
    }

    /**
     * 用户发布动态数量
     */
    static function checkCount($userId = 0, $startTime = '')
    {
        $where = [
            ['user_id', $userId],
            ['status', self::STATUS_OK]
        ];
        if ($startTime) $where[] = ['created_at', '>=', $startTime];
        return self::where($where)->count();
    }

    /**
     * 通过审核
     */
    static function veryfyOK($moment = null)
    {
        if (!$moment) return 0;
        if ($moment->status == self::STATUS_OK) return -1;
        self::where('id', $moment->id)->update(['status' => self::STATUS_OK]);
        $todayCtn = Moment::checkCount($moment->user_id, date('Y-m-d'));
        $momentLmt = config('busi.daily_task.moment_pub_lmt');
        if ($todayCtn >= $momentLmt) {
            //加入动态任务
            $profile = UserProfile::getProfile($moment->user_id);
            $result = DailyTaskLog::addLog($moment->user_id, DailyTaskLog::getTaskId('moment', $profile->role));
        }
        return 1;
    }

    /**
     * 通过不审核
     */
    static function veryfyBad($moment = null)
    {
        if (!$moment) return 0;
        if ($moment->status == self::STATUS_BAD) return -1;
        self::where('id', $moment->id)->update(['status' => self::STATUS_BAD]);
        $pubTime = (string)$moment->created_at;
        $systemMsg = "很抱歉，您{$pubTime}上传的动态审核不通过！
请勿发布以下类型内容~
①无意义的内容、灌水、无内容的动态
②违规言论淫秽、色情、政治敏感内容
③过度传播负面情绪，明显会产生不良印象的内容";
        Message::pushSystem($moment->user_id, $systemMsg);
        return 1;
    }
}
